<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\controller\Image;
use think\Db;
header('Access-Control-Allow-Origin:*');

class Upload extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function up()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        $data = [];
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['size' => 1567800, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        if ($info) {
            $date = date('Ymd');
            $path = $info->getSaveName();
            $ext = $info->getExtension();
            $strs = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
            $name = substr(str_shuffle($strs),mt_rand(0,strlen($strs)-11),10);
            if(!file_exists(ROOT_PATH . 'public' . DS . 'uploads/thumb/'.$date)){
                mkdir(ROOT_PATH . 'public' . DS . 'uploads/thumb/'.$date);
            }
            $str = $date.'/'. $name.'.'.$ext;
            copy('uploads/'.$path,ROOT_PATH . 'public' . '/' . 'uploads/thumb/'.$str);
            $path = str_replace('\\', '/', $path);
            $compress = new Image('uploads/thumb/' . $str, 0.5);//压缩图片
            $compress->compressImg('uploads/thumb/' .$str);//压缩图片
            $data['all_image'] = 'uploads/'.$path;
            $data['thumb_image'] = 'uploads/thumb/'.$str;
            $id = Db::name('images')->insertGetId($data);
            $return = [
                'image_id'=>$id,
                'all_image' => IMG.'/'.$data['all_image'],//原图
                'thumb_image' => IMG.'/'.$data['thumb_image']//缩略图
            ];
            // 成功上传后 获取上传信息
            $this->success('成功', $return, '0');// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
        } else {
            // 上传失败获取错误信息
            $this->success($file->getError(),'','1');
        }
    }
    
    /*
     * 上传图片 内部使用
     * @param $file 图片文件对象
     * */
    public function ups($file)
    {
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['size' => 156780, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        if ($info) {
            $path = $info->getSaveName();
            return $path;
        } else {
            $this->success($file->getError(),'','1');
        }
    }
    /*
     * 师傅端认证,上传各种图片走这个接口
     * */
    public function indent_up()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $type = $this->request->param('types');
        // echo '<pre>';print_r($type);
        $file = $this->request->file('file');
        // echo '<pre>';print_r($file);exit;
        $data = [];
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['size' => 1567800, 'ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        if ($info) {
            $date = date('Ymd');
            $path = $info->getSaveName();
            $ext = $info->getExtension();
            $strs = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm";
            $name = substr(str_shuffle($strs),mt_rand(0,strlen($strs)-11),10);
            if(!file_exists(ROOT_PATH . 'public' . DS . 'uploads/thumb/'.$date)){
                mkdir(ROOT_PATH . 'public' . DS . 'uploads/thumb/'.$date);
            }
            $str = $date.'/'. $name.'.'.$ext;
            copy('uploads/'.$path,ROOT_PATH . 'public' . '/' . 'uploads/thumb/'.$str);
            $path = str_replace('\\', '/', $path);
            $compress = new Image('uploads/thumb/' . $str, 0.5);//压缩图片
            $compress->compressImg('uploads/thumb/' .$str);//压缩图片
            $data['all_image'] = 'uploads/'.$path;
            $data['thumb_image'] = 'uploads/thumb/'.$str;
            $id = Db::name('images')->insertGetId($data);
            $return = [
                'image_id' => $id,
                'all_image' => '/'.$data['all_image'],//原图
                'thumb_image' => '/'.$data['thumb_image'],//缩略图
                'type' => $type
            ];
            // 成功上传后 获取上传信息
            $this->success('成功', $return, '0');// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
        } else {
            // 上传失败获取错误信息
            $this->success($file->getError(),'','1');
        }
    }
}